﻿using EFEntity;
using ISerives;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRDB.Controllers
{
    public class transferController : Controller
    {
        private readonly MyDbContext myDbContext;
        private readonly Iconfig_file_first_kindServices config_file_first_kindSerives;
        private readonly Iconfig_file_second_kindServices config_file_second_kindSerives;
        private readonly Iconfig_file_third_kindServices config_file_third_kindSerives;
        private readonly Imajor_changeServices major_changeSerives;
        private readonly Iconfig_major_kindServices config_major_kindSerives;
        private readonly Iconfig_majorServices config_majorSerives;
        public transferController( MyDbContext myDbContext, 
            Iconfig_file_first_kindServices config_file_first_kindSerives,
            Iconfig_file_second_kindServices config_file_second_kindSerives,
            Iconfig_file_third_kindServices config_file_third_kindSerives,
            Imajor_changeServices major_changeSerives,
            Iconfig_major_kindServices config_major_kindSerives,
            Iconfig_majorServices config_majorSerives  )
        {
            this.myDbContext = myDbContext;
            this.config_file_first_kindSerives = config_file_first_kindSerives;
            this.config_file_second_kindSerives = config_file_second_kindSerives;
            this.config_file_third_kindSerives = config_file_third_kindSerives;
            this.major_changeSerives = major_changeSerives;
            this.config_major_kindSerives = config_major_kindSerives;
            this.config_majorSerives = config_majorSerives;

        }

     

        private async Task yiji()
        {
            List<config_file_first_kind> list = await config_file_first_kindSerives.Select();
            SelectList sl = new SelectList(list, "first_kind_id", "first_kind_name");
            ViewBag.s = sl;
        }
        private async Task erji()
        {
            List<config_file_second_kind> list = await config_file_second_kindSerives.Select();
            SelectList s2 = new SelectList(list, "second_kind_id", "second_kind_name");
            ViewBag.s1 = s2;
        }
        private async Task ysanji()
        {
            List<config_file_third_kind> list = await config_file_third_kindSerives.Select();
            SelectList s3 = new SelectList(list, "third_kind_id", "third_kind_name");
            ViewBag.s2 = s3;
        }
        //职位分类
        private async Task zwconfig_major_kind()
        {
            List<Mconfig_major_kind> list = await config_major_kindSerives.Select();
            SelectList s4 = new SelectList(list, "major_kind_id", "major_kind_name");
            ViewBag.s3 = s4;
        }
        //职位名称
        private async Task mcconfig_majorSerives()
        {
            List<Mconfig_major> list = await config_majorSerives.Select();
            SelectList sl = new SelectList(list, "major_id", "major_name");
            ViewBag.s4 = sl;
        }
        //联动1级查2级
        public async Task<IActionResult> LianDong(string one)
        {
            if (!one.Equals("0"))
            {
                List<Econfig_file_third_kind> list = new List<Econfig_file_third_kind>();
                list = await config_file_third_kindSerives.Selecter(one);

                return Content(JsonConvert.SerializeObject(list));
            }
            else
            {
                return Content(JsonConvert.SerializeObject(0));
            }
        }
        //联动2级查三级
        public async Task<IActionResult> LianDongtwo(string two)
        {
            if (!two.Equals("0"))
            {
                List<Econfig_file_third_kind> list = new List<Econfig_file_third_kind>();
                list = await config_file_third_kindSerives.Selectsan(two);
                return Content(JsonConvert.SerializeObject(list));
            }
            else
            {
                return Content(JsonConvert.SerializeObject(0));
            }
        }
        public async Task<IActionResult> zhiweifenlei(string ii)
        {
            if (!ii.Equals("0"))
            {
                List<config_major> list = new List<config_major>();
                list = await config_majorSerives.Selecter(ii);
                return Content(JsonConvert.SerializeObject(list));
            }
            else
            {
                return Content(JsonConvert.SerializeObject(0));
            }
        }
        //调动
        [HttpGet]
        public async Task<IActionResult> register(int id)
        {
            await yiji();
            await erji();
            await ysanji();
            await zwconfig_major_kind();
            await mcconfig_majorSerives();
            Emajor_change list = await major_changeSerives.selectont(id);
            return View(list);
        }
        [HttpPost]
        public async  Task<IActionResult > register(major_change t)
        {
            t.new_first_kind_name = myDbContext.config_file_third_kind.Where(e => e.first_kind_id == t.new_first_kind_id).FirstOrDefault().first_kind_name;
            t.new_second_kind_name = myDbContext.config_file_third_kind.Where(e => e.second_kind_id == t.new_second_kind_id).FirstOrDefault().second_kind_name;
            t.new_third_kind_name = myDbContext.config_file_third_kind.Where(e => e.third_kind_id == t.new_third_kind_id).FirstOrDefault().third_kind_name;
            t.new_major_kind_name = myDbContext.ZWFL.Where(e => e.major_kind_id == t.new_major_kind_id).FirstOrDefault().major_kind_name;
            t.new_major_name = myDbContext.ZWSZ.Where(e => e.major_id == t.new_major_id).FirstOrDefault().major_name;
            
             Emajor_change list = await major_changeSerives.selectont(t.mch_id);
            Emajor_change e = new Emajor_change()
            {
                mch_id = t.mch_id,
                //first_kind_id = t.first_kind_id,
                //first_kind_name = t.first_kind_name,
                //second_kind_id = t.second_kind_id,
                //second_kind_name = t.second_kind_name,
                //third_kind_id = t.third_kind_id,
                //third_kind_name = t.third_kind_name,
                //major_kind_id = t.major_kind_id,
                //major_kind_name = t.major_kind_name,
                //major_id = t.major_id,
                //major_name = t.major_name,
                new_first_kind_id = t.new_first_kind_id,
                new_first_kind_name = t.new_first_kind_name,
                new_second_kind_id = t.new_second_kind_id,
                new_second_kind_name = t.new_second_kind_name,
                new_third_kind_id = t.new_third_kind_id,
                new_third_kind_name = t.new_third_kind_name,
                new_major_kind_id = t.new_major_kind_id,
                new_major_kind_name = t.new_major_kind_name,
                new_major_id = t.new_major_id,
                new_major_name = t.new_major_name,
                //human_id = t.human_id,
                //human_name = t.human_name,
                //salary_standard_id = t.salary_standard_id,
                //salary_standard_name = t.salary_standard_name,
                //salary_sum = t.salary_sum,
                new_salary_standard_id = t.new_major_id,
                new_salary_standard_name = t.new_major_name,
                new_salary_sum = t.new_salary_sum,
                change_reason = t.change_reason,
                //check_reason = "",
                check_status = 1,
                register = "啊giao",
                //checker = "",
                regist_time = DateTime.Now,
                //check_time = t.check_time,
            };
            //if (t.first_kind_name == t.new_first_kind_name||
            //    t.second_kind_name== t.new_second_kind_name||
            //    t.third_kind_name == t.new_third_kind_name||
            //    t.major_kind_name == t.new_major_kind_name||
            //    t.major_name == t.new_major_name)
            //{
            //    return Content("<script>alert('新机构不能与旧机构一致');window.location.href='/transfer/register'</script>", "textml", Encoding.Default);
            //}
                int num = await major_changeSerives.updeta(e);
            if (num > 0)
            {
                return RedirectToAction("register_success");
            }
            else
            {
                return View(t);
            }
        }
        public IActionResult register_success()
        {
            return View();
        }
        //调动登记查询
        [HttpGet]
        public async Task<IActionResult> register_locate()
        {
            await yiji();
            await erji();
            await ysanji();
            return View();
        }
        [HttpPost]
        public IActionResult register_locate(major_change e)
        {
            return RedirectToAction("register_list");
        }
       //调动查询分页
        [HttpGet]
        public IActionResult register_list(major_change e)
        {
            return View();
        }

        public string where;
        //调动分页
        public async Task<IActionResult> Fy(int id, string a,string b,string c )
        {
            where = $"1=1 and check_status=0 ";
            if(a != "" && a != null) 
            {
                where += $"and first_kind_id like '%{a}%' ";
            }
            if(b != "" && b != null)
            {
                where += $"and second_kind_id like '%{b}%' ";
            }
            if(c != "" && c != null)
            {
                where += $"and third_kind_id like '%{c}%' ";
            }
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                currentPage = id,
                pageSize = 1,
                where = where
            };
            List<major_change> list = await major_changeSerives.Fenye(fy);
            di["list"] = list;
            di["rows"] = fy.rows;
            di["pages"] = fy.pages;
            return Ok(di);
        }
        //查询
        public async Task<IActionResult> locate()
        {
            await yiji();
            await erji();
            await ysanji();
            await zwconfig_major_kind();
            await mcconfig_majorSerives();
            return View();
        }
        public IActionResult list(major_change e)
        {
            return View(); 
        }
        public async Task<IActionResult> Feye(int id, string a, string b, string c, string d, string e)
        {
            //查询条件
            where = $"1=1 and check_status=2";
            if (a != "undefined" && a != null)
            {
                where += $"and new_first_kind_id like '%{a}%' ";
            }
            if (b != "undefined" && b != null)
            {
                where += $"and new_second_kind_id like '%{b}%' ";
            }
            if (c != "undefined" && c != null)
            {
                where += $"and new_third_kind_id like '%{c}%' ";
            }
            if (d != "undefined" && d != null)
            {
                where += $"and new_major_kind_id like '%{d}%' ";
            }
            if (e != "undefined" && e != null)
            {
                where += $"and new_major_id like '%{e}%' ";
            }
            //泛型 Dictionary 用键值对
            //list使用的下标
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                currentPage = id,
                pageSize = 1,
                where = where
            };
            List<major_change> list = await major_changeSerives.Fenye(fy);
            di["list"] = list;
            di["rows"] = fy.rows;
            di["pages"] = fy.pages;
            return Ok(di);
        }
        public async Task<IActionResult> detail(int id)
        {
            Emajor_change list = await major_changeSerives.selectont(id);
            return View(list);
        }
        //审核分页
        public IActionResult check_list()
        {
            return View();
        }
        //审核分页
        public async Task<IActionResult> Fye(int id)
        {
            where = "1=1 and check_status=1";
            //泛型 Dictionary 用键值对
            //list使用的下标
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                currentPage = id,
                pageSize = 1,
                where = where
            };
            List<major_change> list = await major_changeSerives.Fenye(fy);
            di["list"] = list;
            di["rows"] = fy.rows;
            di["pages"] = fy.pages;
            return Ok(di);
        }
        //审核
        [HttpGet]
        public async Task<IActionResult> check(int id)
        {
            Emajor_change list = await major_changeSerives.selectont(id);
            return View(list);
        }
        [HttpPost]
        public async Task<IActionResult> check(major_change t)
        {
            Emajor_change e = new Emajor_change()
            {
                mch_id=t.mch_id,
                check_reason = t.check_reason,
                checker="啊giao",
                check_time=DateTime.Now,
                check_status=t.check_status
            };
            int num = await major_changeSerives.updetaqq(e);
            if (num > 0)
            {
                return RedirectToAction("check_success");
            }
            else
            {
                return View(t);
            }
        }
        public IActionResult check_success()
        {
            return View();
        }

    }
}
